package com.qf.mapper.impl;

import com.qf.mapper.CourseMapper;
import com.qf.pojo.Course;
import com.qf.utils.DBUtils;

import java.sql.SQLException;
import java.util.List;

public class CourseMapperImpl implements CourseMapper {
    /**
     * 添加课程
     *
     * @param name
     */
    @Override
    public void add(String name) {
        try {
            DBUtils.commonUpdate("insert into course(name) values(?)", name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据课程ID删除课程
     *
     * @param id
     */

    @Override
    public void delete(int id) {
        try {
            DBUtils.commonUpdate("delete from course where id = ?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改课程
     *
     * @param id
     * @param name
     */
    @Override
    public void update(int id, String name) {
        try {
            DBUtils.commonUpdate("update course set name = ? where id = ?", name, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据课程ID获取课程
     *
     * @param id
     * @return
     */

    @Override
    public Course getCourse(int id) {
        Course course = null;
        try {
            course = DBUtils.commonQueryObj(Course.class, "select * from course where id = ?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return course;
    }

    /**
     * 根据课程名字获取课程
     *
     * @param name
     * @return
     */
    @Override
    public Course getCourse(String name) {
        Course course = null;
        try {
            course = DBUtils.commonQueryObj(Course.class, "select * from course where name = ?", name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return course;
    }

    /**
     * 获取课程列表
     * @return
     */
    @Override
    public List<Course> getCourses() {

        List<Course> courses = null;
        try {
            courses = DBUtils.commonQueryList(Course.class, "select * from course");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return courses;
    }
}
